package com.szholly.utils.spring.springmvc.route;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.UrlPathHelper;

public class DefaultViewRouteHandler implements HandlerAdapter {

	private static String DefaultPath = "view/";
	
	private UrlPathHelper urlHelper;

	public DefaultViewRouteHandler() {
		urlHelper = new UrlPathHelper();
	}

	@Override
	public long getLastModified(HttpServletRequest request, Object formHandler) {
		return -1;
	}

	@Override
	public ModelAndView handle(HttpServletRequest request,
			HttpServletResponse response, Object formHandler) throws Exception {

		String url = urlHelper.getLookupPathForRequest(request);
		url = url.replace(".do", "");
		if (url.startsWith("/")) {
			url = url.substring(1);
		}
		ModelAndView view = new ModelAndView();
		url = DefaultPath + url;
		view.setViewName(url);
		return view;
	}

	@Override
	public boolean supports(Object obj) {
		if (obj instanceof DefaultViewRouteHandler) {
			return true;
		} else {
			return false;
		}
	}

	public static String getDefaultPath() {
		return DefaultPath;
	}

	public static void setDefaultPath(String defaultPath) {
		DefaultPath = defaultPath;
	}
}
